package com.sos.widget.view.groupTime;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.sos.widget.R;
import com.sos.widget.log.LogUtils;

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/10/20 18:40
 */
public class GroupCountTimeView extends LinearLayout implements Runnable {
    public long countTime = 0;
    public NumberFlipView dayView;
    public NumberFlipView hourView;
    public NumberFlipView minuteView;
    public NumberFlipView secondView;
    public int itemTextColor = 0XFFFFFFFF;
    public int itemBackGroupColor = Color.RED;
    public int itemCenterTextColor = Color.RED;
    public int itemTextSize = 17;
    private boolean start = false;

    public GroupCountTimeView(Context context) {
        super(context);
    }

    public GroupCountTimeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GroupCountTimeView);
//        itemColor=typedArray.getColor(R.styleable.GroupCountTimeView_itemColor,Color.parseColor("333333"));
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GroupCountTimeView);
        itemTextColor = typedArray.getColor(R.styleable.GroupCountTimeView_itemTextColor, itemTextColor);
        itemBackGroupColor = typedArray.getColor(R.styleable.GroupCountTimeView_countBackGroupColor, itemBackGroupColor);
        itemCenterTextColor = typedArray.getColor(R.styleable.GroupCountTimeView_itemCenterTextColor, itemCenterTextColor);
        itemTextSize = typedArray.getInt(R.styleable.GroupCountTimeView_itemTextSize,itemTextSize);
    }

    String[] myFormat = {":", ":", ":", ""};

    public void initView(boolean[] childView, String[] format) {
        if (dayView == null) {
            setOrientation(HORIZONTAL);
            setGravity(Gravity.CENTER);
            dayView = new NumberFlipView(getContext(), itemTextColor,itemTextSize);
            dayView.setNum(360);
            hourView = new NumberFlipView(getContext(), itemTextColor,itemTextSize);
            hourView.setNum(24);
            minuteView = new NumberFlipView(getContext(), itemTextColor,itemTextSize);
            minuteView.setNum(60);
            secondView = new NumberFlipView(getContext(), itemTextColor,itemTextSize);
            secondView.setNum(60);
            dayView.setBackgroundResource(R.drawable.bg_r5_red);
            hourView.setBackgroundResource(R.drawable.bg_r5_red);
            minuteView.setBackgroundResource(R.drawable.bg_r5_red);
            secondView.setBackgroundResource(R.drawable.bg_r5_red);
            dayView.setBackgroundTintList(ColorStateList.valueOf(itemBackGroupColor));
            hourView.setBackgroundTintList(ColorStateList.valueOf(itemBackGroupColor));
            minuteView.setBackgroundTintList(ColorStateList.valueOf(itemBackGroupColor));
            secondView.setBackgroundTintList(ColorStateList.valueOf(itemBackGroupColor));
            if (childView[0]) {
                addView(setLayout(dayView, getDesTextView(format[0])));
            }
            if (childView[1]) {
                addView(setLayout(hourView, getDesTextView(format[1])));
            }
            if (childView[2]) {
                addView(setLayout(minuteView, getDesTextView(format[2])));
            }
            if (childView[3]) {
                addView(setLayout(secondView, getDesTextView(format[3])));
            }
        }

    }

    public View lineView() {
        View view = new View(getContext());
        LayoutParams layoutParams = new LayoutParams((int) getContext().getResources().getDimension(R.dimen.dp_15), (int) getContext().getResources().getDimension(R.dimen.dp_3));
        layoutParams.leftMargin = (int) getContext().getResources().getDimension(R.dimen.dp_5);
        layoutParams.rightMargin = (int) getContext().getResources().getDimension(R.dimen.dp_5);
        view.setLayoutParams(layoutParams);
        view.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bg_f93a6f_r20));
        return view;
    }

    public LinearLayout setLayout(NumberFlipView numberFlipView, TextView textView) {
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams tempParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(tempParams);
        linearLayout.addView(numberFlipView);
        linearLayout.addView(textView);
        return linearLayout;
    }

    public TextView getDesTextView(String des) {
        TextView textView = new TextView(getContext());
        textView.setText(des);
        textView.setPadding(5, 0, 5, 0);
        textView.setTextColor(itemCenterTextColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimension(R.dimen.sp_10));
        textView.setIncludeFontPadding(false);
        return textView;
    }

    public void setCountTime(long time, boolean[] childView) {
        setCountTime(time, childView, null);
    }

    public void setCountTime(long time, boolean[] childView, String[] format) {
        if (!start) {
            start = true;
            countTime = time;
            post(new Runnable() {
                @Override
                public void run() {
                    if (format != null) {
                        myFormat = format;
                    }
                    initView(childView, myFormat);
                    long[] longs = initData(time);
                    dayView.startTime(longs[0]);
                    hourView.startTime(longs[1]);
                    minuteView.startTime(longs[2]);
                    secondView.startTime(longs[3]);
                    postDelayed(GroupCountTimeView.this::run, 1000);

                }
            });

        }

    }


    public long[] initData(long totalSeconds) {
        long day = 0;
        long hour = 0;
        long minute = 0;
        long second = 0;
        boolean dayNotAlready = false;
        boolean hourNotAlready = false;
        boolean minuteNotAlready = false;
        boolean secondNotAlready = false;
        if (totalSeconds > 0) {
            secondNotAlready = true;
            second = totalSeconds;
            if (second >= 60) {
                minuteNotAlready = true;
                minute = second / 60;
                second = second % 60;
                if (minute >= 60) {
                    hourNotAlready = true;
                    hour = minute / 60;
                    minute = minute % 60;
                    if (hour > 24) {
                        dayNotAlready = true;
                        day = hour / 24;
                        hour = hour % 24;
                    }
                }
            }
        }
        long[] arrayTime = {day, hour, minute, second};
        return arrayTime;
    }


    @Override
    public void run() {
        this.countTime--;
        if (this.countTime > 0) {
            long[] longs = initData(countTime);
            long day = longs[0];
            long hour = longs[1];
            long minute = longs[2];
            long second = longs[3];
            int lastDay = dayView.getmFlipNumber();
            int lastHour = hourView.getmFlipNumber();
            int lastMinute = minuteView.getmFlipNumber();
            int lastSecond = secondView.getmFlipNumber();
            if (lastDay != day) {
                dayView.jumpNumber();
            }
            if (lastSecond != second) {
                secondView.jumpNumber();
            }
            if (lastMinute != minute) {
                minuteView.jumpNumber();
            }
            if (lastHour != hour) {
                hourView.jumpNumber();
            }
            if (this.countTime >= 0) {
                postDelayed(GroupCountTimeView.this, 1000);
            }
        } else {
            start = false;
        }

    }


}
